package com.zhongwang.business.module.estimateBudgetAccountingManage.service;

import com.zhongwang.business.base.BaseService;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.gen.entity.BusBudgetAccountingBasicInfoConfigTemplate;
import com.zhongwang.business.gen.entity.BusMachiningEquipmentQuotaVersion;
import com.zhongwang.business.module.estimateBudgetAccountingManage.dao.AccountingDao;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.BusBudgetAccountingBasicInfoConfigModel;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.BusMachiningEquipmentModel;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.MachiningSearchConditionData;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 徐一贺
 * @className BasicInfoConfigService
 * @description 概算预算核算基础信息配置业务逻辑层
 * @date 2020/11/27 10:11
 * @versron ver 1.0
 */
@Log4j2
@Service
public class BasicInfoConfigService extends BaseService {
    @Autowired
    private AccountingDao accountingDao;

    /**
     * basicConfigList 获取概算预算核算基础信息配置列表
     *
     * @param param 检索参数
     * @return java.util.List<com.zhongwang.business.gen.entity.BusDataSourceTemplate>
     * @author 徐一贺
     * @date 2020/11/27 10:30
     */
    public List<BusBudgetAccountingBasicInfoConfigTemplate> basicConfigList(BusBudgetAccountingBasicInfoConfigTemplate param) {
        return accountingDao.findBusBudgetAccountingBasicInfoConfigTemplateByParam(param);
    }

    /**
     * basicConfigUpdateValue 更新基础信息
     *
     * @param param 参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/11/27 10:49
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult basicConfigUpdateValue(BusBudgetAccountingBasicInfoConfigTemplate param) {
        JsonResult result = new JsonResult();
        try {
            BusBudgetAccountingBasicInfoConfigTemplate before = accountingDao.getBusBudgetAccountingBasicInfoConfigTemplateByPrimayKey(param.getId());
            accountingDao.updateBusBudgetAccountingBasicInfoConfigTemplateValueByPrimayKey(param);
            if("Machining quota".equals(before.getCode())){
                accountingDao.updateClearIsDefault();
                accountingDao.updateBusMachiningEquipmentQuotaVersionSetDefaultByVersion(param.getValue());
            }
            logService.saveLog("基础信息配置-概算预算核算基础信息配置", "进行编辑编辑前值是：" + before.getValue() + ",编辑后的值是：" + param.getValue() + ".");
            result.setType(JsonResult.ResultType.SUCCESS);
            result.setMsg("保存基础配置信息成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result.setType(JsonResult.ResultType.ERROR);
            result.setMsg("保存基础配置信息失败！");
        }
        return result;
    }

    /**
     * macQuotaConfigList 获取机加定额列表
     *
     * @param param 检索参数
     * @return java.util.List<com.zhongwang.business.module.estimateBudgetAccountingManage.model.BusMachiningEquipmentModel>
     * @throws
     * @author 徐一贺
     * @date 2020/11/27 11:23
     */
    public List<BusMachiningEquipmentModel> macQuotaConfigList(BusMachiningEquipmentModel param) {
        return accountingDao.findMacQuotaConfigList(param);
    }

    /**
     * loadSearchConditionData 获取机加定额基础信息检索条件数据
     *
     * @return com.zhongwang.business.module.estimateBudgetAccountingManage.model.MachiningSearchConditionData
     * @author 徐一贺
     * @date 2020/11/27 12:48
     */
    public MachiningSearchConditionData loadSearchConditionData() {
        MachiningSearchConditionData dataModel = new MachiningSearchConditionData();
        //设备类型
        dataModel.setNameTypeList(accountingDao.findNameTypeList());
        //工序
        dataModel.setWorkingProcedureList(accountingDao.findWorkingProcedureList());
        //版本号
        dataModel.setVersionList(accountingDao.findVersionLIst());
        return dataModel;
    }

    /**
     * versionList 获取机加定额版本列表
     *
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/11/27 11:16
     */
    public List<BusMachiningEquipmentModel> versionList() {
        return accountingDao.findVersionLIstForAllInfo();
    }

    /**
     * basicConfigSetDefault 设置为此版本号为系统默认
     *
     * @param param 参数
     * @return com.zhongwang.business.common.JsonResult
     * @throws
     * @author 徐一贺
     * @date 2020/11/27 14:01
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult basicConfigSetDefault(BusBudgetAccountingBasicInfoConfigTemplate param) {
        JsonResult result = new JsonResult();
        try {
            String before = accountingDao.getCurrentMacVersion();
            accountingDao.updateClearIsDefault();
            accountingDao.updateIsDefaultByVerId(param);
            String after = accountingDao.getCurrentMacVersion();
            logService.saveLog("基础信息配置-机加定额配置-设置系统默认定额版本号版本号", "进行编辑编辑前版本号是：【" + before + "】,编辑后的版本号是：【" + after + "】。");
            BusBudgetAccountingBasicInfoConfigTemplate macConfigModel = accountingDao.getBusBudgetAccountingBasicInfoConfigTemplateByCode("Machining quota");
            macConfigModel.setValue(after);
            accountingDao.updateBusBudgetAccountingBasicInfoConfigTemplateValueByPrimayKey(macConfigModel);
            result.setType(JsonResult.ResultType.SUCCESS);
            result.setMsg("设置系统默认成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result.setType(JsonResult.ResultType.ERROR);
            result.setMsg("设置系统默认失败！");
        }
        return result;
    }

    /**
     * basicConfigUpdatePrice 修改定额
     *
     * @param param 修改的信息
     * @return com.zhongwang.business.common.JsonResult
     * @throws
     * @author 徐一贺
     * @date 2020/11/27 14:27
     */
    public JsonResult basicConfigUpdatePrice(BusMachiningEquipmentModel param) {
        JsonResult result = new JsonResult();
        try {
            BusMachiningEquipmentModel before = accountingDao.getBusMachiningEquipmentModelByPrimaryKey(param);
            accountingDao.updateBusMachiningEquipmentModelPriceByPrimaryKey(param);
            BusMachiningEquipmentModel after = accountingDao.getBusMachiningEquipmentModelByPrimaryKey(param);
            logService.saveLog("基础信息配置-机加定额配置-修改定额", "型号为：【" + before.getName() + "】,类型为：【" + before.getNameType() + "】,工序为：【" + before.getWorkingProcedure() + "】,版本号为：【" + before.getVersion() + "】的机加定额进行编辑编辑前定额是：【" + before.getPrice() + "】,编辑后的定额是是：【" + after.getPrice() + "】。");
            result.setType(JsonResult.ResultType.SUCCESS);
            result.setMsg("设置系统默认成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result.setType(JsonResult.ResultType.ERROR);
            result.setMsg("设置系统默认失败！");
        }
        return result;
    }

    /**
     * getNewMachiningQuotaConfigList 获取创建定时需要的数据
     *
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/11/27 15:07
     */
    public List<BusMachiningEquipmentModel> getNewMachiningQuotaConfigList() {
        return accountingDao.getNewMachiningQuotaConfigList();
    }

    /**
     * createMacQuotaConfigList 创建新版本积机加数据
     *
     * @param params
     * @return com.zhongwang.business.common.JsonResult
     * @throws
     * @author 徐一贺
     * @date 2020/11/27 17:53
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult createMacQuotaConfigList(String version, Integer isDefault, List<BusMachiningEquipmentModel> params) {
        JsonResult result = new JsonResult();
        try {
            //插入新版本
            BusMachiningEquipmentQuotaVersion versionObject = new BusMachiningEquipmentQuotaVersion();
            versionObject.setVersion(version);
            versionObject.setCode(version);
            accountingDao.insertBusMachiningEquipmentQuotaVersion(versionObject);
            if (isDefault == 1) {
                accountingDao.updateClearIsDefault();
                for (int i = 0; i < params.size(); i++) {
                    params.get(i).setVersion(version);
                    params.get(i).setIsDefault(isDefault);
                    accountingDao.insertBusMachiningEquipment(params.get(i));
                }
                BusBudgetAccountingBasicInfoConfigTemplate macConfigModel = accountingDao.getBusBudgetAccountingBasicInfoConfigTemplateByCode("Machining quota");
                macConfigModel.setValue(version);
                accountingDao.updateBusBudgetAccountingBasicInfoConfigTemplateValueByPrimayKey(macConfigModel);
                logService.saveLog("基础信息配置-机加定额配置-创建新版本机加数据", "新版本机加版本号是：【" + version + "】,并设置成了系统默认。");
            } else {
                for (int i = 0; i < params.size(); i++) {
                    params.get(i).setVersion(version);
                    params.get(i).setIsDefault(isDefault);
                    accountingDao.insertBusMachiningEquipment(params.get(i));
                }
                logService.saveLog("基础信息配置-机加定额配置-创建新版本机加数据", "新版本机加版本号是：【" + version + "】。");
            }
            result.setType(JsonResult.ResultType.SUCCESS);
            result.setMsg("创建新版本机加数据成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result.setType(JsonResult.ResultType.ERROR);
            result.setMsg("创建新版本机加数据失败！");
        }
        return result;
    }

    /**  macQuotaConfigCheckExist 检测积极啊版本号是非存在
     * @param version 版本号
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/11/30 7:53
     */
    public JsonResult macQuotaConfigCheckExist(String version) {
        JsonResult result = new JsonResult();
        try {
            if(accountingDao.getMacQuotaConfigVersionByVersion(version)!=null){
                result.setType(JsonResult.ResultType.SUCCESS);
                result.setMsg("此版本号已存在请重新命名版本号！");
            }else{
                result.setType(JsonResult.ResultType.ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result.setType(JsonResult.ResultType.ERROR);
        }
        return result;
    }

    /**  basicConfigGetMacVersionList 获取版本列表
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/12/1 20:11
     */
    public List<BusMachiningEquipmentQuotaVersion> basicConfigGetMacVersionList() {
        return accountingDao.basicConfigGetMacVersionList();
    }
}
